
#ifndef COMMON_AUTO_LOCK_H_
#define COMMON_AUTO_LOCK_H_

#include "fiber-env/fiber-env.h"
#include <memory>

namespace fasmio { namespace common {

class AutoLock
{
public:
    explicit AutoLock(fiber_env::IMutex* mutex) :
        mutex_(mutex)
    {
        mutex_->Lock();
    }

    explicit AutoLock(std::unique_ptr<fiber_env::IMutex> &mutex_ptr) :
        mutex_(mutex_ptr.get())
    {
        mutex_->Lock();
    }

    explicit AutoLock(std::shared_ptr<fiber_env::IMutex> &mutex_ptr) :
        mutex_(mutex_ptr.get())
    {
        mutex_->Lock();
    }

    ~AutoLock()
    {
        mutex_->Unlock();
    }

private:
    fiber_env::IMutex * const mutex_;
};

}}  // namespace fasmio::common

#endif  // COMMON_AUTO_LOCK_H_

